package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.domain.BuildingManagement;
import com.example.mybatisplus.model.dto.DeleteDTO;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.model.vo.StatVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.StudentService;
import com.example.mybatisplus.model.domain.Student;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author ablbb
 * @since 2024-07-22
 * @version v1.0
 */
@RestController
@RequestMapping("/api/student")
public class StudentController {

    private final Logger logger = LoggerFactory.getLogger( StudentController.class );

    @Autowired
    private StudentService studentService;

    /**
     * 描述：根据Id 查询
     *
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Student  student =  studentService.getById(id);
        return JsonResponse.success(student);
    }

    // 删除post 批量
    @PostMapping("removeByIds")
    public JsonResponse removeByIds(@RequestBody DeleteDTO dto){
        // 返回值表示是否删除成功
        boolean b = studentService.removeByIds(dto.getIds());
        // res.data
        return JsonResponse.success(b);
    }


    @PostMapping("edit")
    public JsonResponse edit(@RequestBody Student student){
        boolean b = studentService.updateById(student);
        return JsonResponse.success(b);
    }
    //分頁
    @GetMapping("pageList")
    public JsonResponse pageList(Student student, PageDTO dto){
        Page<Student> page=studentService.pageList(student,dto);
        return JsonResponse.success(page);
    }

    /**
     * 描述:创建Student
     *
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Student  student) throws Exception {
        studentService.save(student);
        return JsonResponse.success(null);
    }


    @PostMapping("releaseBed")
    public ResponseEntity<?> releaseBed(@RequestBody Student student) {
        Long studentId = student.getId(); // 获取学生的 ID

        // 根据学生 ID 从数据库中获取完整的 Student 对象
        Student studentFromDB = studentService.getById(studentId);

        if (studentFromDB == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到对应的学生信息，无法释放床位");
        }

        // 设置 isLived 字段为 false，表示床位释放
        studentFromDB.setIsLived(false);

        // 执行更新操作
        boolean updateSuccess = studentService.updateById(studentFromDB);

        if (updateSuccess) {
            return ResponseEntity.ok().body("床位释放成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("床位释放失败，请稍后重试");
        }
    }


   /* @GetMapping("/undergraduates")
    public Page<Student> getUndergraduateStudents(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize) {
        return studentService.getStudentsByRole("本科生", pageNo, pageSize);
    }

    @GetMapping("/graduates")
    public Page<Student> getGraduateStudents(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize) {
        return studentService.getStudentsByRole("研究生", pageNo, pageSize);
    }*/


    // 实现一个统计的请求(按学历）（随便写个名字算了）hg
    // 状态均为均为已入住
    @GetMapping("tongji")
    public JsonResponse tongji(){
        List<StatVO> list=studentService.stat();
        return JsonResponse.success(list);
    }

    // 按专业统计(状态均为已入住）hg
    @GetMapping("tongjimajor")
    public JsonResponse tongjimajor(){
        List<StatVO> list=studentService.statmajor();
        return JsonResponse.success(list);
    }

    // 按照年级统计未入住人数(前端实现数据的总和统计）hg
    @GetMapping("statedu")
    public JsonResponse statedu(){
        List<StatVO> list=studentService.statedu();
        return JsonResponse.success(list);
    }
}

